﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Applay.Web
{
    public class ValidationScopeFilter : IActionFilter
    {
        private ValidationScope validationScope;

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(filterContext.Controller is Controller)
            {
                var wrapper = DependencyResolver.Current.GetService<Wrapper>();

                this.validationScope = wrapper.BeginValidation(new ModelValidationState(((Controller)filterContext.Controller).ModelState));
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (this.validationScope != null)
            {
                this.validationScope.Dispose();
                this.validationScope = null;
            }
        } 
    }
}
